using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TVDesigner.GUI.Elements;

namespace TVDesigner.GUI.Controls
{
    public partial class PropertyPanel : UserControl
    {
        private DiagramPanel _FocusDiagram;
        private Element _FocusElement;


        #region Propriedades

        public Element FocusElement
        {
            get { return _FocusElement; }
            set
            {
                _FocusElement = value;

                if (value != null)
                {
                    _PropertyGrid.SelectedObject = value.LogicElement;
                    _ComboElements.SelectedItem = value;
                }
                else
                {
                    _PropertyGrid.SelectedObject = null;
                    _ComboElements.SelectedItem = null;
                }
            }
        }

        public DiagramPanel FocusDiagram
        {
            get { return _FocusDiagram; }
            set
            {
                _FocusDiagram = value;
                _LoadComboElements(value);
            }
        } 
        #endregion

        public PropertyPanel()
        {
            InitializeComponent();
            _ComboElements.DisplayMember = "Name";
        }

        public void ReloadCombo()
        {
            _LoadComboElements(_FocusDiagram);
        }

        #region Private Methods

        private void _LoadComboElements(DiagramPanel diagram)
        {
            if (diagram == null)
            {
                _PropertyGrid.SelectedObject = null;
                _ComboElements.Items.Clear();
                _ComboElements.SelectedItem = null;
            }
            else
            {
                List<Element> elements = diagram.Elements;

                _ComboElements.Items.Clear();

                foreach (Element ele in elements)
                {
                    _ComboElements.Items.Add(ele);
                }

                FocusElement = diagram.FocusElement;
            }
        }

        private void _ComboElements_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_ComboElements.SelectedItem != null)
            {
                _FocusElement = _ComboElements.SelectedItem as Element;
                _PropertyGrid.SelectedObject = _FocusElement.LogicElement;
                _FocusDiagram.DesignPanel.FocusElement = FocusElement;
            }
            else
            {
                _FocusElement = null;
                _PropertyGrid.SelectedObject = null;
            }
        }

        private void _PropertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            _FocusElement.PropertyGridValueChanged(_PropertyGrid, e);
            _PropertyGrid.Refresh();
            _FocusElement.Invalidate();
        }
        #endregion


    }
}
